from django.views.generic.base import View

from .models import Goods

class GoodsView(View):
    def get(self,request):
        json_list=[]
        goods = Goods.objects.all()[:10]
        # for good in goods:
        #     json_dict = {}
        #     json_dict["name"] = good.name
        #     json_dict["category"] = good.category.name
        #     json_dict["market_price"] = good.market_price
        #     json_list.append(json_dict)
        # model_to_dict方式
        # from django.forms.models import model_to_dict
        # for good in goods:
        #     json_dict = model_to_dict(good)
        #     json_list.append(json_dict)
        # 序列化方式

        from django.core import serializers
        json_data = serializers.serialize("json", goods)

        from django.http import HttpResponse,JsonResponse
        return JsonResponse(json_data,safe=False)
        # return HttpResponse(json_data,content_type="application/json")